<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>GET发送ajax请求</button>
    <p></p>
    <script>
        let btn = document.querySelector('button');
        let p = document.querySelector('p');
        // 2. 给按钮添加点击事件
        btn.addEventListener('click', () => {
            let x = new XMLHttpRequest();
            console.log(x.readyState);
            x.onreadystatechange = function(){
                console.log(x.readyState);
                if(x.readyState===4 && x.status === 200){
                    console.log(x.response);
                    // 方式2
                    // let obj = JSON.parse(x.response);
                    // console.log(obj);
                    // p.innerHTML = obj.msg;

                    p.innerHTML = x.response.msg;
                }
            }
           
            x.open('get','http://localhost/jsonserver');

            x.send();
            // 方式1
            x.responseType = 'json';
            
        })
    </script>
</body>

</html>